package com.gx.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.gx.domain.SysMenu;
import com.gx.domain.SysMenuExample;
import com.gx.domain.SysRoleMenu;
import com.gx.id.DefaultIdGenerator;
import com.gx.id.IdGenerator;
import com.gx.mapper.SysMenuMapper;
import com.gx.service.api.SysMenuService;
import com.gx.service.api.SysRoleMenuService;
import com.gx.utils.ResourceToTreeNodeConvert;
import com.gx.utils.StringUtils;
import com.gx.response.tree.BootstrapUiTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    private IdGenerator idGenerator = DefaultIdGenerator.getInstance();

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public int save(SysMenu sysMenu) {
        sysMenu.setId(idGenerator.generateId());
        sysMenu.setCreateDate(new Date());
        return sysMenuMapper.insert(sysMenu);
    }

    @Override
    public List<SysMenu> queryAll() {
        SysMenuExample sysMenuExample = new SysMenuExample();
        return sysMenuMapper.selectByExample(sysMenuExample);
    }

    @Override
    public JSONArray getMenuJsonList() {
        SysMenuExample sysMenuExample = new SysMenuExample();
        List<SysMenu> sysMenus = sysMenuMapper.selectByExample(sysMenuExample);
        final List<SysMenu> supers = sysMenus.stream().filter(sysMenu -> StringUtils.equals("0", sysMenu.getPid().toString())).collect(Collectors.toList());
        sysMenus.removeAll(supers);
        supers.sort(Comparator.comparingInt(SysMenu::getSort));
        JSONArray jsonArr = new JSONArray();
        for (SysMenu sysMenu : supers) {
            SysMenu pMenu = child(sysMenu, sysMenus, 0, 0);
            jsonArr.add(pMenu);
        }
        return jsonArr;
    }

    @Override
    public List<BootstrapUiTreeNode> getSelectMenuTree() {
        SysMenuExample sysMenuExample = new SysMenuExample();
        List<SysMenu> sysMenus = sysMenuMapper.selectByExample(sysMenuExample);
        List<SysMenu> rootNodesList = ResourceToTreeNodeConvert.getRootNodesList(sysMenus);
        List<BootstrapUiTreeNode> bootstrapUiTreeNodes = ResourceToTreeNodeConvert.convertClassListToSelectBootstrapUiTreeNodeList(rootNodesList, sysMenus);
        return bootstrapUiTreeNodes;
    }

    @Override
    public List<BootstrapUiTreeNode> getCheckedMenuTree(Long roleId) {
        SysMenuExample sysMenuExample = new SysMenuExample();
        List<SysMenu> sysMenus = sysMenuMapper.selectByExample(sysMenuExample);

        List<SysRoleMenu> sysRoleMenus = sysRoleMenuService.queryMenuSysRoleMenu(roleId);
        List<String> menuIdList = sysRoleMenus.stream().map(roleMenu -> roleMenu.getMenuId().toString()).collect(Collectors.toList());

        List<SysMenu> rootNodesList = ResourceToTreeNodeConvert.getRootNodesList(sysMenus);
        List<BootstrapUiTreeNode> bootstrapUiTreeNodes = ResourceToTreeNodeConvert.convertClassListToBootstrapUiTreeNodeList(rootNodesList, sysMenus, menuIdList);
        return bootstrapUiTreeNodes;
    }


    @Override
    public List<SysMenu> getUserMenuList(String sysUserId) {
        SysMenuExample sysMenuExample = new SysMenuExample();
        // TODO 获取当前用户权限Menu
        return sysMenuMapper.selectByExample(sysMenuExample);
    }

    @Override
    public int update(SysMenu sysMenu) {
        SysMenuExample sysMenuExample = new SysMenuExample();
        SysMenuExample.Criteria criteria = sysMenuExample.createCriteria();
        criteria.andIdEqualTo(sysMenu.getId());
        return sysMenuMapper.updateByExample(sysMenu,sysMenuExample);
    }

    public SysMenu child(SysMenu sysMenu, List<SysMenu> sysMenus, Integer pNum, Integer num) {
        List<SysMenu> childSysMenu = sysMenus.stream().filter(s ->
                s.getPid().equals(sysMenu.getId())).collect(Collectors.toList());
        sysMenus.removeAll(childSysMenu);
        SysMenu m;
        for (SysMenu menu : childSysMenu) {
            ++num;
            m = child(menu, sysMenus, pNum, num);
            sysMenu.addChild(menu);
        }
        return sysMenu;
    }


}
